package com.ebeitech.auction.domain;

import com.ebeitech.auction.db.annotation.*;
import com.ebeitech.auction.domain.dao.ChatMessageDao;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.util.CommonUtil;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.StringUtil;

import java.io.Serializable;
import java.util.List;

/**
 * Created by alice on 2017/11/4.
 */
@Table(name = "ChatMessageTable")
public class ChatMessage implements Serializable {

	/**
	 * 消息类型-普通消息
	 */
	public static final String MESSAGE_TYPE_NORMAL = "normalMessage";
	/**
	 * 消息类型-命令消息
	 */
	public static final String MESSAGE_TYPE_COMMAND_SEND = "commandSend";
	/**
	 * 消息类型-确认命令消息
	 */
	public static final String MESSAGE_TYPE_COMMAND_COMFIRM = "confirmCommand";
	/**
	 * 消息类型-本方报价消息
	 */
	public static final String MESSAGE_TYPE_OUR_PRICE = "ourPrice";
	/**
	 * 消息类型-竞方报价消息
	 */
	public static final String MESSAGE_TYPE_THEIR_PRICE = "theirPrice";
	/**
	 * 消息类型-阶段切换消息
	 */
	public static final String MESSAGE_TYPE_PHASE_SWITCH = "phaseSwitch";
	/**
	 * 消息类型-本方报价撤回消息
	 */
	public static final String MESSAGE_TYPE_OUR_PRICE_RECALL = "priceRecall";

	/**
	 * 指令类型-放弃
	 */
	public static final String COMMAND_TYPE_GIVEUP = "0";
	/**
	 * 指令类型-加一手
	 */
	public static final String COMMAND_TYPE_ADD = "1";
	/**
	 * 消息状态-发送中
	 */
	public static final String MESSAGE_STATE_SENDING = "0";

	/**
	 * 消息状态-发送成功
	 */
	public static final String MESSAGE_STATE_SUCCESS = "1";

	/**
	 * 消息状态-发送失败
	 */
	public static final String MESSAGE_STATE_FAILURE = "2";

	private int _id;

	/**
	 * 消息Id
	 */
	private String messageId;

	/**
	 * 消息类型
	 */
	private String messageType;
	/**
	 * 发送人Id
	 */
	private String fromUserId;
	/**
	 * 发送人Account
	 */
	private String fromUserAccount;
	/**
	 * 发送人名字
	 */
	private String fromUserName;
	/**
	 * 时间，毫秒数
	 */
	private String sendTime;
	/**
	 * 发送内容
	 */
	private String messageDetail;
	/**
	 * 直播间Id
	 */
	private String auctionId;
	/**
	 * 命令类型(0:放弃 1:加一手)
	 */
	private String commandType;

	/**
	 * 命令Id
	 */
	private String commandId;

	/**
	 * 竞价Id
	 */
	private String bidId;

	/**
	 * 竞价撤回Id
	 */
	private String recalledBidId;

	/**
	 * 当前阶段最高价
	 */
	private String price;

	/**
	 * 我方出价比例(我方出价,竞方出价发送)
	 */
	private String rate;

	/**
	 * 我方出价比例(撤回消息发送)
	 */
	private String phaseOurResult;

	/**
	 * 我方当前阶段最高价
	 */
	private String phaseOurResultRate;

	/**
	 * 我方当前阶段最高价比例
	 */
	private String phaseResultRate;

	/**
	 * 消息前缀名
	 */
	private String titleText;

	/**
	 * 阶段Id
	 */
	private String phaseId;

	private String bidderName;//竞价方Name
	private String auctionBidderCode;//竞价方Code
	private String nowPhaseId;//当前阶段Id
	/**
	 * 下一段阶段Id
	 */
	private String nextPhaseId;

	/**
	 * 消息状态
	 * 看消息是否是发送状态,发送状态的话,该消息会有转圈发送的效果
	 */
	private String messageState;

	/**
	 * 本阶段正在出价的内容,是个数组,需要把这个标签到\n标红
	 */
	private List<String> nowPahseResultName;
	private String nowPhaseResultNames;

	@Id
	@Column(name = "_id")
	public int get_id() {
		return _id;
	}

	public void set_id(int _id) {
		this._id = _id;
	}

	@PrimaryKey
	@Column(name = "messageId")
	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	@Column(name = "messageType")
	public String getMessageType() {
		return messageType;
	}

	public void setMessageType(String messageType) {
		this.messageType = messageType;
	}

	@Column(name = "fromUserId")
	public String getFromUserId() {
		return fromUserId;
	}

	public void setFromUserId(String fromUserId) {
		this.fromUserId = fromUserId;
	}

	@Column(name = "fromUserName")
	public String getFromUserName() {
		return fromUserName;
	}

	public void setFromUserName(String fromUserName) {
		this.fromUserName = fromUserName;
	}

	@Column(name = "fromUserAccount")
	public String getFromUserAccount() {
		return fromUserAccount;
	}

	public void setFromUserAccount(String fromUserAccount) {
		this.fromUserAccount = fromUserAccount;
	}

	@Column(name = "sendTime")
	public String getSendTime() {
		return sendTime;
	}

	public void setSendTime(String sendTime) {
		this.sendTime = sendTime;
	}

	@Column(name = "messageDetail")
	public String getMessageDetail() {
		return messageDetail;
	}

	public void setMessageDetail(String messageDetail) {
		this.messageDetail = messageDetail;
	}

	@Column(name = "auctionId")
	public String getAuctionId() {
		return auctionId;
	}

	public void setAuctionId(String auctionId) {
		this.auctionId = auctionId;
	}

	@Column(name = "commandType")
	public String getCommandType() {
		return commandType;
	}

	public void setCommandType(String commandType) {
		this.commandType = commandType;
	}

	@Column(name = "commandId")
	public String getCommandId() {
		return commandId;
	}

	public void setCommandId(String commandId) {
		this.commandId = commandId;
	}

	@Column(name = "bidId")
	public String getBidId() {
		return bidId;
	}

	public void setBidId(String bidId) {
		this.bidId = bidId;
	}

	@Column(name = "recalledBidId")
	public String getRecalledBidId() {
		return recalledBidId;
	}

	public void setRecalledBidId(String recalledBidId) {
		this.recalledBidId = recalledBidId;
	}

	public String getRate() {
		return rate;
	}

	public void setRate(String rate) {
		this.rate = rate;
	}

		@Column(name = "price")
	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	@Column(name = "rate")
	public String getPhaseResultRate() {
		return phaseResultRate;
	}

	public void setPhaseResultRate(String phaseResultRate) {
		this.phaseResultRate = phaseResultRate;
	}

	@Column(name = "phaseOurResult")
	public String getPhaseOurResult() {
		return phaseOurResult;
	}

	public void setPhaseOurResult(String phaseOurResult) {
		this.phaseOurResult = phaseOurResult;
	}

	@Column(name = "phaseOurResultRate")
	public String getPhaseOurResultRate() {
		return phaseOurResultRate;
	}

	public void setPhaseOurResultRate(String phaseOurResultRate) {
		this.phaseOurResultRate = phaseOurResultRate;
	}

	@Column(name = "phaseId")
	public String getPhaseId() {
		return phaseId;
	}

	public void setPhaseId(String phaseId) {
		this.phaseId = phaseId;
	}

	public String getBidderName() {
		return bidderName;
	}

	public void setBidderName(String bidderName) {
		this.bidderName = bidderName;
	}

	public String getAuctionBidderCode() {
		return auctionBidderCode;
	}

	public void setAuctionBidderCode(String auctionBidderCode) {
		this.auctionBidderCode = auctionBidderCode;
	}

	public String getNowPhaseId() {
		return nowPhaseId;
	}

	public void setNowPhaseId(String nowPhaseId) {
		this.nowPhaseId = nowPhaseId;
	}

	@Column(name = "nextPhaseId")
	public String getNextPhaseId() {
		return nextPhaseId;
	}

	public void setNextPhaseId(String nextPhaseId) {
		this.nextPhaseId = nextPhaseId;
	}

	@Column(name = "messageState", defaultValue = MESSAGE_STATE_SUCCESS)
	public String getMessageState() {
		return messageState;
	}

	public void setMessageState(String messageState) {
		this.messageState = messageState;
	}

	@Column(name = "titleText")
	public String getTitleText() {
		return titleText;
	}

	public void setTitleText(String titleText) {
		this.titleText = titleText;
	}

	@Column(name = "nowPahseResultNames")
	public String getNowPhaseResultNames() {
		return nowPhaseResultNames;
	}

	public void setNowPhaseResultNames(String nowPhaseResultNames) {
		this.nowPhaseResultNames = nowPhaseResultNames;
	}

	public List<String> getNowPahseResultName() {
		return nowPahseResultName;
	}

	public void setNowPahseResultName(List<String> nowPahseResultName) {
		this.nowPahseResultName = nowPahseResultName;
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof ChatMessage)) {
			return false;
		}
		return StringUtil.getDefaultString(messageId).equals(((ChatMessage) o).getMessageId());
	}

	public void getNewSendMessage() {
		ChatMessage chatMessage = this;
		SPManager spManager = SPManager.getInstance();
		String mUserId = StringUtil.getDefaultString(spManager.get(SPConstants.USER_ID, ""));
		String mUserName = StringUtil.getDefaultString(spManager.get(SPConstants.USER_NAME, ""));
		String mUserAccount = StringUtil.getDefaultString(spManager.get(SPConstants.USER_ACCOUNT, ""));

		chatMessage.setMessageId(CommonUtil.getUUID());
		chatMessage.setSendTime(System.currentTimeMillis() + "");
		chatMessage.setFromUserId(mUserId);
		chatMessage.setFromUserName(mUserName);
		chatMessage.setFromUserAccount(mUserAccount);
		chatMessage.setMessageState(ChatMessage.MESSAGE_STATE_SENDING);
	}

	public void setSendFail() {
		setSendFail(true);
	}

	public void setSendFail(boolean isUpdateDB) {
		final ChatMessage chatMessage = this;
		chatMessage.setMessageState(ChatMessage.MESSAGE_STATE_FAILURE);
		new Thread(new Runnable() {
			@Override
			public void run() {
				ChatMessageDao.getInstance().insertOrUpdate(chatMessage);
			}
		}).start();
	}

}
